<?php

$tb = "abc_cde";
$arr = implode("_", $tb);
$c = count($arr);
$cl = "";
for ($i = 0; $i < $c; $i++) {
	$cl .= ucfirst($arr[$i]);
}

$daoClass = $cl."Dao";
BaseDao::includeOnceTarget("app/".$daoClass);

$dao = new $daoClass($db);
//$dao->addLimit(10);
$list = $dao->select();
try {
	foreach ($list as $r) {
		$dao = new $daoClass($db);

		$desc = $dao->_getDesc();
		foreach ($desc as $k => $inf) {
			if($inf['type'] == 'number') $dao->addColVal($k, $r[$k]);
			else $dao->addColValStr($k, $r[$k]);
		}

		$dao->insert($mysql = true);

	}
} catch (Exception $e) {
}

// baseDao

public function insert($isLog = false, $isMysql = false, $table = "") {
	parent::insert($isMysql, $table);
	if (!$isLog)	$this->writeUpdateLog(Dao::ACTION_TYPE_INSERT);
}
//baseDao

//Dao
	public function insert($isMysql = false, $table="") {
		$sql = $this->getInsertStatement($table);

		if($isMysql) {
			$conn = mysql_connect('localhost', 'root', '');
			mysql_select_db('adptg_total2', $conn);
			return $result = mysql_query($sql);
		}
		return $this->_db->execute($sql);
	}
//Dao

	// LsCreativeRegistLogic  cho adptg
	function _validate(&$db_tot, &$form) {

		var_dump("hee");

		////

	$tb_list = array(
		 "banner                ",
"campaign              ",
"campaign_prev         ",
"campaign_sub          ",
"connect_status_history",
"conversion_tracker    ",
"conversion_tracker_prev",
"creative              ",
"creative_prev         ",
"cv_log                ",
"cv_log_bk             ",
"cv_point              ",
"cv_support_log        ",
"excluded_word         ",
"keyword               ",
"keyword_prev          ",
"link                  ",
"link_device           ",
"mapping_page          ",
"page                  ",
"promotion             ",
"summary               ",
"summary_10            ",
"summary_10_cv         ",
"summary_10_cv_hour    ",
"summary_10_hour       ",
"summary_11            ",
"summary_11_cv         ",
"summary_11_cv_hour    ",
"summary_11_hour       ",
"summary_12            ",
"summary_12_cv         ",
"summary_12_cv_hour    ",
"summary_12_hour       ",
"summary_14            ",
"summary_14_cv         ",
"summary_14_cv_hour    ",
"summary_14_hour       ",
"sync_error            "
		);

		$cc = count($tb_list);
		$xx = 38;

		//for ($i = 0; $i < $cc; $i++) {
			$tb = $tb_list[$xx];
			$tb = trim($tb);
			var_dump($tb);
			$arr = explode("_", $tb);
			$c = count($arr);

			// get class name from table
			$daoClass = "";
			for ($i = 0; $i < $c; $i++) {
				$daoClass .= ucfirst($arr[$i]);
			}
			//var_dump($cl);
			//$daoClass = $daoClass . "Dao";
			//BaseDao::includeOnceTarget("system/" . $daoClass);
			$daoClass = "BannerDao";

			$dao = new $daoClass($db_tot);
			$list = $dao->selectStatement("select * from " . $tb . "limit 10", true);
			if(empty($list)) {
				$list = $dao->selectStatement("desc " . $tb);
				//var_dump($list);
				$desc = array();
				foreach ($list as $r) {
					$desc[$r["Field"]] = array("type"=>strpos($r["Type"], "int") !== false ? "number" : "text");
				}
				//var_dump("desc");
				//var_dump($desc);
				var_dump("hehe insert");


				$dao = new $daoClass($db_tot);
				$_limit = " limit 1200";
				//$_limit = "";

				$list = $dao->selectStatement("select * from " . $tb . $_limit);
				try {
					foreach ($list as $r) {
						$dao = new $daoClass($db_tot);

						foreach ($desc as $k => $inf) {
							if($inf['type'] == 'number') $dao->addColVal($k, $r[$k]);
							else $dao->addColValStr($k, $r[$k]);
						}
						$dao->insert(false,true, $tb);
					}
				} catch (Exception $e) {
				}
			} else {
				var_dump("hehe");
			}
		//} // end for




	// LsCreativeRegistLogic
function run(){
		BaseForm::includeOnceTarget("report/ReportBaseForm");

		BaseDao::includeOnceTarget("system/UserDao");
		BaseDao::includeOnceTarget("system/ReportReserveDao");
		BaseDao::includeOnceTarget("system/ReportFileDao");
		BaseDao::includeOnceTarget("total/BannerDao");

		$form = $this->getForm();
		$db_sys = $this->connectDbSystem();

		//

/*
		$tb_list = array(
//		"calendar" ,
		"carrier"
		,
		"client"
		,"company"
		,"connect_status_history"
		,"cv_index"
		,"cv_name",
"dashboard",
"dashboard_report",
"department",
"device",
"device_model",
"env",
"error_log",
"error_msg",
"inquiry",
"job_log",
"mapping_word",
"marin_reserve",
"master",
"master_data",
"master_history",
"media",
"media_kensyo",
"model",
"notice",
"platform",
"process_log",
"report_file", "report_reserve",
"request",
"request_name",
"service",
"site","summary","timer",
"update_log",
"user","user_req",
"word","word_test",
"zbk_account",
"zbk_summary",
"zbk_user"

);
*/
		$tb_list = array(
		 "banner                ",
"campaign              ",
"campaign_prev         ",
"campaign_sub          ",
"connect_status_history",
"conversion_tracker    ",
"conversion_tracker_prev",
"creative              ",
"creative_prev         ",
"cv_log                ",
"cv_log_bk             ",
"cv_point              ",
"cv_support_log        ",
"excluded_word         ",
"keyword               ",
"keyword_prev          ",
"link                  ",
"link_device           ",
"mapping_page          ",
"page                  ",
"promotion             ",
"summary               ",
"summary_10            ",
"summary_10_cv         ",
"summary_10_cv_hour    ",
"summary_10_hour       ",
"summary_11            ",
"summary_11_cv         ",
"summary_11_cv_hour    ",
"summary_11_hour       ",
"summary_12            ",
"summary_12_cv         ",
"summary_12_cv_hour    ",
"summary_12_hour       ",
"summary_14            ",
"summary_14_cv         ",
"summary_14_cv_hour    ",
"summary_14_hour       ",
"sync_error            "
		);

		$cc = count($tb_list);
		$xx = 0;

		//for ($i = 0; $i < $cc; $i++) {
			$tb = $tb_list[$xx];
			$tb = trim($tb);
			var_dump($tb);
			$arr = explode("_", $tb);
			$c = count($arr);

			// get class name from table
			$daoClass = "";
			for ($i = 0; $i < $c; $i++) {
				$daoClass .= ucfirst($arr[$i]);
			}
			//var_dump($cl);
			//$daoClass = $daoClass . "Dao";
			//BaseDao::includeOnceTarget("system/" . $daoClass);
			$daoClass = "BannerDao";

			$dao = new $daoClass($db_sys);
			$list = $dao->selectStatement("select * from " . $tb . "limit 10", true);
			if(empty($list)) {
				$list = $dao->selectStatement("desc " . $tb);
				//var_dump($list);
				$desc = array();
				foreach ($list as $r) {
					$desc[$r["Field"]] = array("type"=>strpos($r["Type"], "int") !== false ? "number" : "text");
				}
				//var_dump("desc");
				//var_dump($desc);
				var_dump("hehe insert");


				$dao = new $daoClass($db_sys);
				$_limit = " limit 1200";
				//$_limit = "";

				$list = $dao->selectStatement("select * from " . $tb . $_limit);
				try {
					foreach ($list as $r) {
						$dao = new $daoClass($db_sys);

						foreach ($desc as $k => $inf) {
							if($inf['type'] == 'number') $dao->addColVal($k, $r[$k]);
							else $dao->addColValStr($k, $r[$k]);
						}
						$dao->insert(false,true, $tb);
					}
				} catch (Exception $e) {
				}
			} else {
				var_dump("hehe");
			}
		//} // end for


	// LsBulkReserveLogic


	// LsBulkReserveLogic
